<?php
class Default_Model_TourCategory extends Zend_Db_Table {
	protected $_name    = 'tour_category';
	protected $_primary = 'id';
	protected $_dependentTables = array('Default_Model_MenuTour');
	
	
	// creat item in select box
	public function itemInSelectbox(){
		$db = Zend_Registry::get('connectDb');
		//$db = Zend_Db::factory($adapter,$config);
		
	    $select = $db->select()
		             ->from('tour_category AS c',array('id','category_name'))
		             ->order('c.order ASC');
	    
		$result  = $db->fetchPairs($select);
		//array_unshift($result,'Root Menu');
	
		return $result;
	
	}
	
	public function countItem($arrParam = null, $options = null) {
		$db = Zend_Registry::get('connectDb');
		$ssFilter = $arrParam['ssFilter'];
		$select = $db->select()
		             ->from('tour_category AS c', array('COUNT(c.id)  AS totalItem'));
		
		if(!empty($ssFilter['keywords'])){
			$keywords = '%' . $ssFilter['keywords'] . '%';
			$select->where('c.category_name LIKE ?',$keywords,STRING);
		}
		
		$result = $db->fetchOne($select);
		return $result;
		
	}
	
	public function listItem($arrParam = null, $options = null) {
		if($options['task'] == 'admin-list') {
			$db = Zend_Registry::get('connectDb');
			$paginator = $arrParam['paginator'];
			$ssFilter = $arrParam['ssFilter'];
			//$db = Zend_Db::factory($adapter,$config);
	    	$select = $db->select()
		             ->from('tour_category AS c', array('id', 'category_name', 'status', 'order', 'type'))
		             ->joinLeft('tours AS t', 'c.id = t.tour_category_id', array('COUNT(t.id) AS tours'))
		             ->group('c.id');
		            
		    if(!empty($ssFilter['col']) && !empty($ssFilter['order'])) {
	    		$select->order($ssFilter['col'] . ' ' . $ssFilter['order']);
	    	}
	    	
	    	if($paginator['itemCountPerPage']>0) {
	    		$page     = $paginator['currentPage'];
	    		$rowCount = $paginator['itemCountPerPage'];
	    		$select->limitPage($page,$rowCount);
	    	}
	    	
	    	// seach keyword
	    	if(!empty($ssFilter['keywords'])){
	    		$keywords = '%' . $ssFilter['keywords'] . '%';
	    		$select->where('c.category_name LIKE ?',$keywords,STRING);
	    	}
	    	$result = $db->fetchAll($select);
		}
	
		return $result;
	}
	
 // change status
   public function changeStatus($arrParam = null, $options = null) {
   	   
   	   // Multi change
   	   $cid = $arrParam['cid'];
   	   if(count($cid) > 0) {
   	        if($arrParam['type'] == 1){
				$status = 1;
			}else{
				$status = 0;
			}
			
			$ids   = implode(',', $cid);
			$where = 'id IN (' . $ids . ')';
			$data  =  array('status'=>$status);
			$this->update($data, $where);
   	   }
   	   
   	   // change only one item
   	   if($arrParam['id'] > 0) {
	        $status = $arrParam['type'];
	        $data   = array('status'=>$status);
	        $where  = 'id = ' . $arrParam['id'];
	        $this->update($data, $where);
   	   }	
   }
   
   // orderItems
   public function sortItems($arrParam = null, $options = null) {
       $order = $arrParam['order'];
   	   $cid   = $arrParam['cid'];
   	   if(count($cid) > 0) {
   		    foreach ($cid as $key => $value) {
   			    $where = 'id = ' . $value;
   			    $data  = array('order' => $order[$value]);
   			    $this->update($data, $where);
   		    }
   		 
   	   }
   }
   
   // delete item
   public function deleteItem($arrParam = null, $options = null) {
   	   if($options['task'] == 'admin-delete') {
   		   $where = ' id = ' . $arrParam['id'];
   		   $this->delete($where);
   	   }
   	 
       if($options['task'] == 'admin-multi-delete') {
   		   $cid = $arrParam['cid'];
   		   if(count($cid) > 0) {
   			   $ids   = implode(',', $cid);
   			   $where = 'id IN (' . $ids . ')';
   			   $this->delete($where);
   		   }
   	   }
   }
   
   // save item
   public function saveItem($arrParam = null, $options = null) {
	   	if($options['task'] == 'admin-add') {
	   		$row = $this->fetchNew();
	   		$row->category_name = $arrParam['category_name'];
            $row->created 		= date("Y-m-d");
	   		$row->created_by 	= 1;
	   		$row->status 		= $arrParam['status'];
	   		$row->order 		= $arrParam['order'];
	   		$row->type          = $arrParam['type'];
	   		
	   		$arrParam['category_id'] = $row->save();
	   		
	   		$menuTour = new Default_Model_MenuTour();
	   		$menuTour->saveItem($arrParam, array('task' => 'admin-add'));
	   	}
	   	 
	   	if($options['task'] == 'admin-edit') {
	   		$id    = $arrParam['id'];
	   		$where = 'id = ' . $id;
	   
	   		$row =  $this->fetchRow($where);
	   
	   		$row->category_name = $arrParam['category_name'];
	   		$row->modified      = date("Y-m-d");
	   		$row->modified_by   = 1;
	   		$row->status 		= $arrParam['status'];
	   		$row->order 		= $arrParam['order'];
	   		$row->type          = $arrParam['type'];
	   		
	   		$row->save();
	   		
	   		$menuTour = new Default_Model_MenuTour();
	   		if($arrParam['menu'] > 0){
	   			$menuTour->saveItem($arrParam, array('task'=>'admin-edit'));
	   		}else {
	   			if($arrParam['menu'] == 0){
	   				$menuTour->deleteItem($arrParam, array('task'=>'admin-delete-category'));
	   			}
	   		}
	   }
   }
    
   // get info item
   public function getItem($arrParam = null, $options = null) {
   	   if(($options['task'] == 'admin-info') || ($options['task'] == 'admin-edit')) {
   		   $where  = 'id = ' . $arrParam['id'];
   		   $result = $this->fetchRow($where)->toArray();
   		   
   		   // display menu
   		   $result['menu'] = $this->getMenu($arrParam['id']);
       }
       
       return $result;
   }
   
   
   // find menu contain category
   public function getMenu($id){
   	   if(isset($id)){
   	   	   $categoryRowset = $this->find($id);
   	   	   $category       = $categoryRowset->current();
   	   	   $menuRowset     = $category->findDefault_Model_SubMenuViaDefault_Model_MenuTour();
   	   	   $result         = $menuRowset->toArray();
   	   	   return $result;
   	   }
   }
}